"""
仅仅是综合练习，对装饰器知识点的综合应用
要实现的内容是：
    1.方法执行的时长较长，后续执行时，直接返回固定的值--即缓存装饰器
    2.记录函数执行的耗时
    3.使用类装饰器和方法装饰器两种方式混合实现
"""
import time
from functools import wraps


class CacheDecorator:

    def __init__(self, fun):
        self.__cache_dic = {}
        self.fun = fun
        self.__name__ = fun.__name__

    def __call__(self, *args, **kwargs):
        if self.__cache_dic.get(self.fun.__name__) is not None:
            return self.__cache_dic[self.fun.__name__]
        else:
            result = self.fun(*args, **kwargs)
            self.__cache_dic[self.fun.__name__] = result
            return result


def cost_time(fun):
    # @wraps(fun)
    def inner_func(*args, **kwargs):
        start_time = time.time()
        result = fun(*args, **kwargs)
        end_time = time.time()
        print(f'{fun.__name__}执行耗时{end_time - start_time}')
        return result

    return inner_func




@cost_time
@CacheDecorator
def func():
    print('fun start')
    time.sleep(3)
    return 999


print(func())
print(func())
